package com.company.project.controller.banner;

import com.company.project.entity.SysDept;
import com.company.project.service.DeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Date;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.SysBannerEntity;
import com.company.project.service.SysBannerService;



/**
 * banner管理
 *
 * @author YangYulin
 * @email YangYulinit@163.com
 * @date 2020-08-14 20:23:48
 */
@Controller
@RequestMapping("/")
public class SysBannerController {
    @Autowired
    private SysBannerService sysBannerService;

    @Autowired
    private DeptService deptService;

    /**
    * 跳转到页面
    */
    @GetMapping("/index/sysBanner")
    public String sysBanner() {
        return "sysbanner/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("sysBanner/add")
    @RequiresPermissions("sysBanner:add")
    @ResponseBody
    public DataResult add(@RequestBody SysBannerEntity sysBanner){
        sysBanner.setDeptId(deptService.rootDept());
        sysBanner.setCreateTime(new Date());
        sysBannerService.save(sysBanner);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("sysBanner/delete")
    @RequiresPermissions("sysBanner:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        sysBannerService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("sysBanner/update")
    @RequiresPermissions("sysBanner:update")
    @ResponseBody
    public DataResult update(@RequestBody SysBannerEntity sysBanner){
        sysBannerService.updateById(sysBanner);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("sysBanner/listByPage")
    @RequiresPermissions("sysBanner:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody SysBannerEntity sysBanner){
        Page page = new Page(sysBanner.getPage(), sysBanner.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper();
        //查询条件示例
        queryWrapper.eq("dept_id",deptService.rootDept());
        queryWrapper.orderByAsc("sortr");
        queryWrapper.orderByAsc("create_time");
        IPage<SysBannerEntity> iPage = sysBannerService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
